﻿


///	<summary>
///	站点相关
///	</summary>
var Site = {};
Site.IsDebug = false;
///	<summary>
///	跟据 ID 获取元素
///	</summary>
Site.i$ = function (o) { if (typeof (o) == "object") { return o; } else { return document.getElementById(o); } }
///	<summary>
///	跟据 TagName 获取元素
///	</summary>
Site.t$ = function (o) { if (typeof (o) == "object") { return o; } else { return document.getElementsByTagName(o); } }
///	<summary>
///	跟据 Name 获取元素集合
///	</summary>
Site.n$ = function (o) { if (typeof (o) == "object") { return o; } else { return document.getElementsByName(o); } }

Site.getJSON = function (url, data, callback) {
    $.ajax({
        type: "GET",
        cache: false,
        url: url,
        data: data,
        success: callback,
        dataType: 'json'
    });
}

///	<summary>
///	彩种相关
///	</summary>
Site.Lottery = {};

///	<summary>
///	当前彩种
///	</summary>
Site.Lottery.CurrentLottery = { ID: null, Name: null, OpenCodeLength: null };

///	<summary>
///	期号相关
///	</summary>
Site.Issue = {};

///	<summary>
///	当前期的内容
///	</summary>
Site.Issue.IssueState = { Normal: 0, Opend: 1 };

///	<summary>
///	当前期的内容
///	</summary>
Site.Issue.CurrentIssue = null;

///	<summary>
///	上一期的内容
///	</summary>
Site.Issue.LastIssue = null;

Site.Issue.LastIssueID = null;

///	<summary>
///	是否正在进行倒计时
///	</summary>
Site.Issue.IsTimeStart = false;

///	<summary>
///	指示是否播放声音
///	</summary>
Site.Issue.IsPlaySound = true;

///	<summary>
///	播放声音的列表
///	</summary>
Site.Issue.Sounds = [{ Check: function (span) {
    var hours = span.getHours();
    var minute = span.getMinutes();
    var second = span.getSeconds();
    var date = span.getDate();
    if (!Site.Issue.IsPlaySound)
        this.Stop();
    return Site.Issue.IsPlaySound && !playsound && date == 1 && hours == 0 && minute == 0;
}, Play: function () {
    if (!Site.Issue.IsPlaySound)
        this.Stop();
    if (playsound)
        return;
    $("#divplaySound").load(Site.ServerPath + "Content/Sound.htm");
    playsound = true;
},
    Stop: function () { $("#divplaySound").html(""); }
}];

///	<summary>
///	获取上一期的开奖号码
///	</summary>
Site.Issue.GetLastIssue = function () {
    if (Site.Issue.LastIssue != null)
        return;
    Site.getJSON(Site.ServerPath + "Play/LastWinNumber/" + Site.Lottery.CurrentLottery.ID, null, function (jsondata) {
        try {
            if (jsondata == null)
                return;

            $("#lastIssueName")[0].innerHTML = "第" + jsondata.Number + "期";
            $("#waittimespan span")[0].innerHTML = "第" + jsondata.Number + "期正在开奖...";

            if (Site.Issue.WaitNumList == null) {
                Site.i$("tbwinnostate").style.display = "none";
                Site.i$("waittimespan").style.display = "block";
                var len = 5 - Site.Lottery.CurrentLottery.OpenCodeLength;
                var winnums = $("#openwinnumber span");
                var tmp = winnums.splice(0, len);
                $.each(tmp, function (i, n) {
                    n.innerText = " ";
                });
                Site.Issue.WaitNumList = winnums;  //开始滚动数字
                Site.Issue.WaitNextWinNumber();
            }

            if (jsondata.State == Site.Issue.IssueState.Normal)
                return;

            Site.Issue.LastIssue = {};
            Site.Issue.LastIssue.IssueID = jsondata.ID;
            Site.Issue.LastIssue.IssueName = jsondata.Number;
            Site.Issue.LastIssue.WinNumber = jsondata.OpenCode;
            Site.Issue.LastIssue.State = jsondata.State;

            Site.Issue.UpdateIssueWin();

        } catch (e) {
            if (Site.IsDebug)
                alert(e);
        }
    });

    window.setTimeout(Site.Issue.GetLastIssue, 5000);
}

Site.Issue.setKjXiTai = function (s) {
    var a = s.split("");
    Site.i$("xt_3x").innerHTML = "";
    Site.i$("xt_2xh").innerHTML = "";
    Site.i$("xt_3xh").innerHTML = "";
    Site.i$("xt_dxds").innerHTML = "";

    if (Site.Lottery.CurrentLottery.ID == 9) {
        $("#waittimespan").show();
        $("#tbwinnostate").hide();
        $("#waittimespan .waitnexttxt").text(s);
    } else if (Site.Lottery.CurrentLottery.OpenCodeLength == 3) {
        if (a[1] == a[2] || a[1] == a[3] || a[2] == a[3])
            Site.i$("xt_3x").innerHTML = "组三";
        else
            Site.i$("xt_3x").innerHTML = "组六";
    } else if (Site.Lottery.CurrentLottery.OpenCodeLength == 5) {

        //三星
        var z = "";
        if (a[2] == a[3] || a[2] == a[4] || a[3] == a[4]) {
            z = "组三";
        } else {
            z = "组六";
        }
        Site.i$("xt_3x").innerHTML = z;
        //二星和
        Site.i$("xt_2xh").innerHTML = a[3] * 1 + a[4] * 1;
        // 三星和
        Site.i$("xt_3xh").innerHTML = a[3] * 1 + a[4] * 1 + a[2] * 1;
        var aa, b, c, d;
        if (a[3] * 1 < 6) { aa = "小"; } else { aa = "大"; }
        if (a[3] * 1 % 2 == 0) { b = "双"; } else { b = "单"; }
        if (a[4] * 1 < 6) { c = "小"; } else { c = "大"; }
        if (a[4] * 1 % 2 == 0) { d = "双"; } else { d = "单"; }
        Site.i$("xt_dxds").innerHTML = aa + c + "&nbsp;" + aa + d + "&nbsp;" + b + c + "&nbsp;" + b + d;
    }
}


/// <sumary>
/// 更新开奖号码
/// </sumary>
Site.Issue.UpdateIssueWin = function () {
    if (Site.Issue.LastIssue == null)
        return;

    Site.Issue.UpdateWinNumber();
}

var tempwinnumber = 0;
Site.Issue.UpdateWinNumber = function () {
    Site.Issue.WaitNumList = [];
    var tmparray = [];
    var len = 5 - Site.Lottery.CurrentLottery.OpenCodeLength;
    var winnums = $("#openwinnumber span");
    var tmp = winnums.splice(0, len);
    $.each(tmp, function (i, n) {
        n.innerText = " ";
    });
    var nums = [];
    if (Site.Lottery.CurrentLottery.ID == 9)
        nums = Site.Issue.LastIssue.WinNumber.replace(/,/g, "").split('');
    else if (Site.Issue.LastIssue.WinNumber.indexOf(",") >= 0)
        nums = Site.Issue.LastIssue.WinNumber.split(',');
    else if (Site.Issue.LastIssue.WinNumber.indexOf(" ") >= 0)
        nums = Site.Issue.LastIssue.WinNumber.split(' ');
    else
        nums = Site.Issue.LastIssue.WinNumber.split('');

    $.each(winnums, function (i, n) {
        if (i > tempwinnumber) {
            tmparray.push(n);
        } else {
            n.innerText = nums[i];
        }
    });
    if (tmparray.length == 0) {
        Site.i$("tbwinnostate").style.display = "block";
        Site.i$("waittimespan").style.display = "none";
        Site.Issue.setKjXiTai(Site.Issue.LastIssue.WinNumber);
        Site.Issue.WaitNumList = null;
        tempwinnumber = 0;
        var dd = $("#divoldissue");
        if (dd.length > 0)
            dd.load(Site.ServerPath + "Issues/HistoryLeft/" + Site.Lottery.CurrentLottery.ID + "?tp=" + new Date() / 1000);
        return;
    }
    tempwinnumber++;
    Site.Issue.WaitNumList = tmparray;
    window.setTimeout(Site.Issue.UpdateWinNumber, 1000);
}




Site.Issue.WaitNumList = null;

var tempnum = 0;
/// <sumary>
/// 等待下一期开奖
/// </sumary>
Site.Issue.WaitNextWinNumber = function () {
    if (Site.Issue.WaitNumList == null)
        return;
    $.each(Site.Issue.WaitNumList, function (i, n) {
        n.innerText = tempnum;
    });
    tempnum++;
    if (tempnum >= 10)
        tempnum = 0;
    window.setTimeout(Site.Issue.WaitNextWinNumber, 70);
}

///	<summary>
///	取得当前种彩正在进行的期号
///	</summary>
Site.Issue.GetCurrentIssue = function () {
    if (Site.Issue.CurrentIssue != null)
        return;
    Site.getJSON(Site.ServerPath + "Play/CurrentIssue/" + Site.Lottery.CurrentLottery.ID, null, function (jsondata) {
        try {
            if (jsondata == null) {
                Site.Issue.LockCountDown();
                return;
            }

            Site.Issue.CurrentIssue = {};
            Site.Issue.CurrentIssue.IssueID = jsondata.ID;
            Site.Issue.CurrentIssue.IssueName = jsondata.Number;
            Site.Issue.CurrentIssue.StartTime = new Date(jsondata.StartTime);
            Site.Issue.CurrentIssue.EndTime = new Date(jsondata.EndTime);
            Site.Issue.CurrentIssue.NowTime = new Date(jsondata.NowTime);
            Site.Issue.NextStart = new Date(jsondata.NextStart);
            Site.Issue.CurrentIssue.LoadTime = new Date();

            $("#currentIssueName").each(function (i) {
                this.innerHTML = "第" + jsondata.Number + "期剩余投注时间";
            });
            //开始倒计时
            if (!Site.Issue.IsTimeStart) {
                $(".vediotime span").css("display", "block");
                $(".vediotime img").css("display", "none");
                Site.Issue.CountDownTime();
            }
        }
        catch (e) { if (Site.IsDebug) alert("Site.Issue.GetCurrentIssue():" + e); }
    });

    window.setTimeout(Site.Issue.GetCurrentIssue, 5000);
}

///	<summary>
///	本期投注已截止
///	</summary>
Site.Issue.LockCountDown = function () {
    Site.Issue.IsTimeStart = false;
    $(".vediotime span").css("display", "none");
    $(".vediotime img").css("display", "block");
}

///	<summary>
///	本期投注倒计时
///	</summary>
Site.Issue.CountDownTime = function () {
    try {
        if (Site.Issue.CurrentIssue == null)
            return;
        if (Site.Issue.IsTimeStart == false)
            return;
        var d1 = new Date();
        var tmp = (Site.Issue.CurrentIssue.EndTime - Site.Issue.CurrentIssue.NowTime) - (d1 - Site.Issue.CurrentIssue.LoadTime);

        if (tmp <= 0) {  //倒计时结束
            playsound = false;
            $.each(Site.Issue.Sounds, function (i, n) { //停止播发声音
                n.Stop()
            });
            Site.Issue.LockCountDown();
            Site.Issue.WaitNextIssue();
            return;
        } else if (tmp > 0 || tmp < 20) {
            Site.Issue.IsTimeStart = true;
        } else {
            Site.Issue.IsTimeStart = false;
        }
        window.setTimeout(Site.Issue.CountDownTime, 1000);     //间隔一秒种
        var span = new Date("1-1-1990");
        span.setSeconds(tmp / 1000);
        var timestr = "";
        var isred = false;
        var hours = span.getHours();
        var minute = span.getMinutes();
        var second = span.getSeconds();
        var date = span.getDate();

        $.each(Site.Issue.Sounds, function (i, n) {
            if (n.Check(span))
                n.Play();
        });

        if (hours > 0 || date > 1) {      //间隔超过1小时
            if (hours < 10)
                timestr += "0" + hours + ":";
            else
                timestr += hours + ":";
            if (minute < 10)
                timestr += "0" + minute;
            else
                timestr += minute;
            timestr += "<br /><font style='font-size:18px'>";
            if (second < 10)
                timestr += "0" + second;
            else
                timestr += second;
            timestr += " s</font>";
        } else if (minute == 0) {     //间隔不足1分钟
            timestr = "00:";
            if (second < 10)
                timestr += "0" + second;
            else
                timestr += second;
            isred = true;
        } else {                                  //间隔在小时时以内
            if (minute < 10)
                timestr += "0" + minute + ":";
            else
                timestr += minute + ":";
            if (second < 10)
                timestr += "0" + second;
            else
                timestr += second;
        }

        $(".vediotime span").each(function (item) {
            this.innerHTML = timestr;
            if (isred)
                this.style.color = "Red";
            else
                this.style.color = "Yellow";
        });

    } catch (e) {
        if (Site.IsDebug)
            alert("Site.Issue.CountDown():" + e);
    }
}
var playsound = false;
///	<summary>
///	等待下一期的开始
///	</summary>
Site.Issue.WaitNextIssue = function () {
    try {
        if (Site.Issue.CurrentIssue == null)
            return;
        var d1 = new Date();
        var tmp = (Site.Issue.NextStart - Site.Issue.CurrentIssue.EndTime) - (d1 - Site.Issue.CurrentIssue.LoadTime);
        if (tmp <= 0) {  //倒计时结束

            $("#currentIssueName").each(function (i) {
                this.innerHTML = "载入中...";
            });
            Site.Issue.CurrentIssue = null; //加载一期
            Site.Issue.GetCurrentIssue();
            Site.Issue.LastIssue = null;    //加载这期的开奖号码

            Site.i$("tbwinnostate").style.display = "none";
            Site.i$("waittimespan").style.display = "block";

            if (Site.Issue.WaitNumList == null) {
                var len = 5 - Site.Lottery.CurrentLottery.OpenCodeLength;
                var winnums = $("#openwinnumber span");
                var tmp = winnums.splice(0, len);
                $.each(tmp, function (i, n) {
                    n.innerText = " ";
                });
                Site.Issue.WaitNumList = winnums;  //开始滚动数字
                Site.Issue.WaitNextWinNumber();
            }
            Site.Issue.GetLastIssue();
            return;
        }

        window.setTimeout(Site.Issue.WaitNextIssue, 1000);      //间隔一秒种
        var span = new Date("1-1-1990");
        span.setSeconds(tmp / 1000);
        var timestr = "";
        var hours = span.getHours();
        var minute = span.getMinutes();
        var second = span.getSeconds();
        var date = span.getDate();
        var strmsg = "本期投注已经截止距离下期：";

        if (hours > 0 || date > 1) {      //间隔超过1小时
            if (hours < 10)
                timestr += "0" + hours + ":";
            else
                timestr += hours + ":";
            if (minute < 10)
                timestr += "0" + minute + ":";
            else
                timestr += minute + ":";
            if (second < 10)
                timestr += "0" + second;
            else
                timestr += second;
            strmsg = "本期投注已经截止下期：";
        } else if (minute == 0) {     //间隔不足1分钟
            timestr = "00:";
            if (second < 10)
                timestr += "0" + second;
            else
                timestr += second;
        } else {                                  //间隔在小时时以内
            if (minute < 10)
                timestr += "0" + minute + ":";
            else
                timestr += minute + ":";
            if (second < 10)
                timestr += "0" + second;
            else
                timestr += second;
        }
        $("#currentIssueName").each(function (i) {
            this.innerHTML = strmsg + timestr;
        });
    } catch (e) {
        if (Site.IsDebug)
            alert("Site.Issue.WaitNextIssue():" + e);
    }
}
